package com.hiwhitley.easy.day01;

import java.util.Stack;
/**
 * 设计一个有getMin功能的栈
 * 题目要求：
 * 实现一个特殊的栈，在实现栈的基本功能的基础上，在实现返回栈中最小元素的操作
 */

/**
 * Created by hiwhitley on 2016/5/23.
 */
public class MyStack1 {
	
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    public MyStack1(){
        stackData = new Stack<>();
        stackMin = new Stack<>();
    }

    public void push(int newNum) {
        if (stackMin.isEmpty()) {
            stackMin.push(newNum);
        } else if (newNum <= getMin()) {
            stackMin.push(newNum);
        }

        stackData.push(newNum);

    }

    public int pop() {
        if (stackData.isEmpty()) {
            throw new RuntimeException("Your stack is empty");
        }
        int value = stackData.pop();
        if (value == this.getMin()) {
            stackMin.pop();
        }
        return value;
    }

    public int getMin() {
        if (stackMin.isEmpty()) {
            throw new RuntimeException("Your stack is empty");
        }
        return stackMin.peek();
    }

    @Override
    public String toString() {
        return "MyStack1{" +
                "stackData=" + stackData +
                '}';
    }
}
